<template>
  <div class="sidebar">
    <el-menu :default-active="activeIndex" class="el-menu-vertical" @select="handleMenuClick">
      <el-menu-item index="1">
        <el-icon><House /></el-icon>
        <span>发现</span>
      </el-menu-item>
      <el-menu-item index="2">
        <el-icon><ShoppingCart /></el-icon>
        <span>购物车</span>
      </el-menu-item>
      <el-menu-item index="3">
        <el-icon><Star /></el-icon>
        <span>动态</span>
      </el-menu-item>
      <el-menu-item index="4">
        <el-icon><Bell /></el-icon>
        <span>通知</span>
      </el-menu-item>
      <el-menu-item index="5">
        <el-icon><Plus /></el-icon>
        <span>发布</span>
      </el-menu-item>
      <el-menu-item index="6">
        <el-icon><Money /></el-icon>
        <span>领券中心</span>
      </el-menu-item>
      <el-menu-item v-if="isLoggedIn" index="12" @click="goToProfile">
        <el-icon><User /></el-icon>
        <span>个人中心</span>
      </el-menu-item>
      <el-menu-item v-else index="13" class="login-menu-item" @click="goToLogin">
        <span class="login-menu-text">登录</span>
      </el-menu-item>
    </el-menu>
    <div class="expand-menu" @click="showExpand = !showExpand">
      <el-icon><Operation /></el-icon>
      <span class="expand-menu-text">更多</span>
    </div>
    <transition name="slide-fade">
      <div v-if="showExpand" class="expand-content">
        <el-menu>
          <el-menu-item index="7">关于</el-menu-item>
          <el-menu-item index="8">隐私协议</el-menu-item>
          <el-menu-item index="9">帮助与客服</el-menu-item>
          <el-menu-item index="10">设置</el-menu-item>
          <el-menu-item index="11">深色模式</el-menu-item>
        </el-menu>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
  /**
   * 文件说明
   * 侧边栏组件 Home首页侧边切换栏目
   */
  import { ref, onMounted } from 'vue'
  import { useRouter } from 'vue-router'
  import {
    House,
    ShoppingCart,
    Star,
    Bell,
    Plus,
    Money,
    Operation,
    User,
  } from '@element-plus/icons-vue'
  import { useProfileStore } from '@/store/profile'

  const showExpand = ref(false)
  const isLoggedIn = ref(false) // 登录状态 暂时使用本地状态，后续可以从props或store获取
  const router = useRouter()

  const goToLogin = () => {
    router.push('/login')
  }

  //点击跳转到个人中心
  const goToProfile = () => {
    // router.push('/profile')
  }

  //点击不同的menu-item 切换不同的页面
  import { computed } from 'vue'
  import { useRoute } from 'vue-router'

  const route = useRoute()
  /**
   * 路由跳转映射表
   * 键为菜单项的index，值为路由的name
   */
  const routeMap = {
    '1': 'home-default',
    '2': 'shoppingcart',
    '3': 'trends',
    '4': 'inform',
    '5': 'publish',
    '6': 'promotion',
  } as const

  // 根据 routeMap 创建一个反转的映射对象，其中键是路由名称，值是原始的索引
  const routeToIndexMap = Object.fromEntries(
    Object.entries(routeMap).map(([index, name]) => [name, index]),
  )

  const activeIndex = computed(() => {
    // 根据当前路由名称计算活动索引，如果路由名称不在映射中则默认返回 '1'
    return routeToIndexMap[route.name as string] || '1'
  })

  /**
   * click方法 点击菜单项时触发
   * 跳转到相应的路由
   * @param index 菜单项的索引
   */
  const handleMenuClick = (index: string) => {
    // 根据点击的菜单索引跳转到相应的路由
    router.push({
      name: routeMap[index as keyof typeof routeMap] || 'home-default',
      params: { from: 'sidebar' },
    })
  }

  onMounted(() => {
    const profileStore = useProfileStore()
    isLoggedIn.value = profileStore.isLoggedIn
  })
</script>

<style scoped>
  .login-menu-item {
    width: 90%;
    border-radius: 25px;
    font-weight: bold;
    text-align: center;
    color: #fff;
    font-size: 20px;
    height: 50px;
    line-height: 50px;
    background-color: #4b8edc;
  }
  .login-menu-text {
    padding-left: 40%;
  }

  .login-menu-item:hover {
    color: #000000;
    background-color: #ffdb46;
  }
  .el-menu-vertical .el-menu-item {
    border-radius: 25px !important;
    font-weight: bold;
    text-align: center;
    font-size: 16px;
    height: 60px;
    line-height: 50px;
    padding: 0 20px;
    margin: 8px 0;
  }
  .sidebar {
    width: 200px;
    top: 0;
    bottom: 0;
    background-color: #fff;
    box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
  }

  .el-menu {
    border-right: none;
  }

  .expand-menu {
    text-align: left;
    padding: 10px 30px;
    cursor: pointer;
    bottom: 0;
    width: 100%;
    margin-top: 10px;
    justify-content: left;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: 600;
  }
  .expand-menu-text {
    font-weight: bold;
    text-align: center;
    font-size: 16px;
    height: 60px;
    line-height: 50px;
    padding: 0 10px;
    position: relative;
    top: -2px;
  }

  .expand-content {
    width: 200px;
    top: 100%;
    left: 0;
    background-color: #fff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  }

  .slide-fade-enter-active {
    transition: all 0.3s ease-out;
  }

  .slide-fade-leave-active {
    transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
  }

  .slide-fade-enter-from,
  .slide-fade-leave-to {
    transform: translateY(-10px);
    opacity: 0;
  }
</style>
